package com.ruoyi.am.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.am.domain.AmDriving;
import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.am.domain.AmErrors;
import com.ruoyi.am.service.IAmErrorsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 错误日志Controller
 * 
 * @author hkkk
 * @date 2020-12-21
 */
@Controller
@RequestMapping("/am/errors")
public class AmErrorsController extends BaseController
{
    private String prefix = "am/errors";

    @Autowired
    private IAmErrorsService amErrorsService;

    @RequiresPermissions("am:errors:view")
    @GetMapping()
    public String errors()
    {
        return prefix + "/errors";
    }

    /**
     * 查询错误日志列表
     */
    @RequiresPermissions("am:errors:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AmErrors amErrors)
    {
        startPage();
        List<AmErrors> list = amErrorsService.selectAmErrorsList(amErrors);
        return getDataTable(list);
    }

    /**
     * 导出错误日志列表
     */
    @RequiresPermissions("am:errors:export")
    @Log(title = "错误日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AmErrors amErrors)
    {
        List<AmErrors> list = amErrorsService.selectAmErrorsList(amErrors);
        ExcelUtil<AmErrors> util = new ExcelUtil<AmErrors>(AmErrors.class);
        return util.exportExcel(list, "errors");
    }

    /**
     * 新增错误日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存错误日志
     */
    @RequiresPermissions("am:errors:add")
    @Log(title = "错误日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AmErrors amErrors)
    {
        return toAjax(amErrorsService.insertAmErrors(amErrors));
    }

    /**
     * 修改错误日志
     */
    @GetMapping("/edit/{errorId}")
    public String edit(@PathVariable("errorId") Long errorId, ModelMap mmap)
    {
        AmErrors amErrors = amErrorsService.selectAmErrorsById(errorId);
        mmap.put("amErrors", amErrors);
        return prefix + "/edit";
    }

    /**
     * 修改保存错误日志
     */
    @RequiresPermissions("am:errors:edit")
    @Log(title = "错误日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AmErrors amErrors)
    {
        return toAjax(amErrorsService.updateAmErrors(amErrors));
    }

    /**
     * 删除错误日志
     */
    @RequiresPermissions("am:errors:remove")
    @Log(title = "错误日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(amErrorsService.deleteAmErrorsByIds(ids));
    }

    @PostMapping("/createError")
    @ResponseBody
    public AjaxResult createError(String msg){

        JSONObject object=JSONObject.parseObject(msg);
        AmErrors amErrors=JSONObject.toJavaObject(object, AmErrors.class);
        amErrors.setCreateTime(DateUtils.getNowDate());
        return toAjax(amErrorsService.insertAmErrors(amErrors));

    }

}
